<?php 
	session_start();
include_once '../pushing.php';

if ($pushTime == true) {
	if ($_SESSION['UserId'] != 3)
		header('Location: push.php');
}
	
	if(!isset($_SESSION['UserId']))
	{
		header('Location: login.php');
   		die();
	}
	
	# ini_set('display_errors', '1'); 
	
	require_once '../mysqlConnect.php'; //mysqli database connect
	require_once '../Functions/ViewPatientController.php';
	
function getPatientInfobyID($id) //returns all fields from patient table
{
	$conn = GetConnection();
	$sql = "SELECT * FROM Patient WHERE ID ='$id'";
	$value = $conn->query($sql);
	$rs = $value->fetch_assoc();
	$conn->close();
	return $rs;	
}

function getTestName($testId)
{
	$conn = GetConnection();
	$sql = "SELECT Name FROM Test WHERE ID = '$testId'";
	$value = $conn->query($sql);
	$rs = $value->fetch_assoc();
	$real = $rs['Name'];
	$conn->close();
	return $real;
}

function getMedicationById($id) {
	$conn = GetConnection();
	$results = $conn->query("SELECT Name FROM Medication where ID = " . $id);
	$rs = $results->fetch_assoc();
	$return = $rs['Name'];
	$conn->close();
	return $return;
}

function getMedicationNames() {
	$conn = GetConnection();
	$results = $conn->query("SELECT * FROM Medication");
	$conn->close();
	return $results;
}


function getDropDownOptions ($id) {
	$conn = GetConnection();
	$sql = "SELECT * FROM answer_options WHERE id = '$id' ORDER BY type";
	$sqlResults = $conn->query($sql);
	$conn->close();
	return $sqlResults;
}

# get type options via a question table 
function gettype_options($qnumber, $tablename) {
	$conn = GetConnection();
	$sql = "SELECT type_options from " . $tablename . " WHERE qnumber = " . $qnumber;
	$results = $conn->query($sql);
	$rs = $results->fetch_assoc();
	$conn->close();
	$options = getDropDownOptions($rs['type_options']);
	$returnArray = array();
	while ($res = $options->fetch_assoc())
		$returnArray[] = $res['type'];
	return $returnArray;
}

$patientInfo = getPatientInfobyID($_REQUEST['PatId']);

	       	
$conn = GetConnection();


	//ICD9 information
	{		 
		$icd_objects = fetchICDObjects($_GET['PatId']);
		
		$icd_codes = retrieveAllICDObjects();
	}
	
	//answer descriptions
	{
		$sqlAnswerDesc = "SELECT * FROM answer_options";
		$descriptions = $conn->query($sqlAnswerDesc);
		
		while ($desc = $descriptions->fetch_assoc())
		$answerDescriptions[$desc['type']] = $desc['description'];
	}

	//diagnostic test information
	{
		$sqlColumnNames = "SELECT * FROM medical_test_questions ORDER BY qnumber";
		$columnNames1 = $conn->query($sqlColumnNames);
		        	
		$sqlAnswers = "SELECT * FROM medical_test_answers WHERE patient_id = " . $_GET['PatId'] . " ORDER BY record_date desc";
		$answers1 = $conn->query($sqlAnswers);
		
		$diagnosticInformationArray = array();
		while ($row = $answers1->fetch_assoc()) {
			$answerOptions1 = array();
			$answerOptions2 = array();
			$options1 = getDropDownOptions(6);
			while ($opt1 = $options1->fetch_assoc())
				$answerOptions1[] = $opt1['type'];
			$options2 = getDropDownOptions(7);
			while ($opt2 = $options2->fetch_assoc())
				$answerOptions2[] = $opt2['type'];
			
			$diagnosticInformationArray[] = array(array("name" => "diagnostic" . $row['id'] . "1", "num" => 1, "ans" => $row['result1'], "options" => $answerOptions1), array("name" => "diagnostic" . $row['id'] . "2", "num" => 2, "ans" => $row['result2'], "options" => $answerOptions2), $row);
		}
	}
	
	
	//prescription information
	{
		$sqlColumnNames = "SELECT * FROM prescription_questions ORDER BY qnumber";
        $columnNames2 = $conn->query($sqlColumnNames);
        	
        $sqlAnswers = "SELECT * FROM prescription_records WHERE patient_id = " . $_GET['PatId'] . " ORDER BY end_date desc";
        $answers2 = $conn->query($sqlAnswers);
        
        $prescriptionInformationArray = array();
        while ($row = $answers2->fetch_assoc()) {
        	$id = $row['id'];
        	$medicationID = $row['medication_id'];
        	$dosage = $row['dosage'];
        	$startdate = $row['start_date'];
        	$enddate = $row['end_date'];
        	
        	$infoCombined = array('id' => $id, 'medid' => $medicationID, 'dosage' => $dosage, 'startdate' => $startdate, 'enddate' => $enddate);
        	$prescriptionInformationArray[] = $infoCombined;
        }
	}
	        	

	//psychological information
	{
		$sqlColumnNames = "SELECT * FROM psychological_test_questions ORDER BY qnumber";
        $columnNames3 = $conn->query($sqlColumnNames);
        	
        $sqlAnswers = "SELECT * FROM psychological_test_answers WHERE patient_id = " . $_GET['PatId'] . " ORDER BY record_date desc";
        $answers3 = $conn->query($sqlAnswers);
        
        $psychologicalInformationArray = array();
        while($row = $answers3->fetch_assoc()) {
        	#get drop down information for questions
        	$answerOptions3 = array();
        	$options = getDropDownOptions(3);
			while ($opt1 = $options->fetch_assoc())
				$answerOptions3[] = $opt1['type'];
				
			$answerOptions8 = array();
			$options = getDropDownOptions(8);
			while ($opt8 = $options->fetch_assoc())
				$answerOptions8[] = $opt8['type'];	
				
			$answerOptions9 = array();	
			$options = getDropDownOptions(9);
			while ($opt9 = $options->fetch_assoc())
				$answerOptions9[] = $opt9['type'];
				
			$globalOptions = array();	
        	$optionsglobal = getDropDownOptions(4);
			while ($opt2 = $optionsglobal->fetch_assoc())
				$globalOptions[] = $opt2['type'];
        	
        	$id = $row['id'];        	
        	$qnumber1 = array($row['qnumber1'], $answerOptions8);
        	$qnumber2 = array($row['qnumber2'], $answerOptions9); 
        	$qnumber3 = array($row['qnumber3'], $answerOptions3); 
        	$global = array($row['global'], $globalOptions);
        	$record_date = $row['record_date'];
        	
        	$infoCombined = array('id' => $id, 'qnumber1' => $qnumber1, 'qnumber2' => $qnumber2, 'qnumber3' => $qnumber3, 'global' => $global, 'record_date' => $record_date);
        	$psychologicalInformationArray[] = $infoCombined;
        }
	}
	
	     	
	//physical symptoms
	{
		$sqlColumnNames = "SELECT * FROM physical_symptom_questions ORDER BY qnumber";
        $columnNames4 = $conn->query($sqlColumnNames);
        	
        $sqlAnswers = "SELECT * FROM physical_symptom_answers WHERE patient_id = " . $_GET['PatId'] . " ORDER BY record_date desc";
        $answers4 = $conn->query($sqlAnswers);
        
        $physicalSymptomInformationArray = array();
        while ($row = $answers4->fetch_assoc()) {
        	
        	$id = $row['id'];
        	$patientId = $row['patient_id'];
        	
        	$answerOptions = array();
        	# loop through the array, excluding the id, patient_id, and the record_date
        	for ($i = 1; $i <= count($row) - 3; $i++) {
        		$options = gettype_options($i, 'physical_symptom_questions');
        		$answerOptions[] = array('qnumber' => $i, 'answer' => $row['qnumber' . $i], 'options' => $options);
        	}
        	     	
        	$infoCombined = array('id' => $id, 'patient_id' => $patientId, 'record_date' => $row['record_date'], 'answers' => $answerOptions);
        	$physicalSymptomInformationArray[] = $infoCombined;	
        }
	}

	
	//neuropsychological symptom information
	{
		$sqlColumnNames = "SELECT * FROM neuropsychological_symptom_questions ORDER BY qnumber";
		$columnNames5 = $conn->query($sqlColumnNames);
	        	
		$sqlAnswers = "SELECT * FROM neuropsychological_symptom_answers WHERE patient_id = " . $_GET['PatId'] . " ORDER BY record_date desc";
		$answers5 = $conn->query($sqlAnswers);
		
		$neuroPsychologicalSymptomsArray = array();
		
		while ($row = $answers5->fetch_assoc()) {
			
			$id = $row['id'];
			$patientId = $row['patient_id'];
			
			$answerOptions = array();
			# loop through the array, excluding the id, patient_id, and the record_date
			for ($i = 1; $i <= count($row) - 3; $i++) {
				$options = gettype_options($i, 'neuropsychological_symptom_questions');
				$answerOptions[] = array('qnumber' => $i, 'answer' => $row['qnumber' . $i], 'options' => $options);
			}

			$infoCombined = array('id' => $id, 'patient_id' => $patientId, 'record_date' => $row['record_date'], 'answers' => $answerOptions);
			$neuroPsychologicalSymptomsArray[] = $infoCombined;
		}
	}

$conn->close();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Patient</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js" type="text/javascript"></script>

<script src="../JavaScript/js_basic_functions.js" type="text/javascript"></script>
<script src="../JavaScript/edit_patient.js" type="text/javascript"></script>

<script src="../JavaScript/jQueryTabs/js/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="../JavaScript/jQueryTabs/js/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="../JavaScript/jQueryTabs/css/custom-theme/jquery-ui-1.8.16.custom.css"/>
<link rel="stylesheet" type="text/css" href="../Styler.css"/>


<script type="text/javascript">
$(document).ready(function(){
	
	setCollapseBind("basicInfoClick", "BasicInfocollapse");
	setCollapseBind("diagnosticInfoClick", "diagnosticInfoCollapse");
	setCollapseBind("prescriptionInfoClick", "prescriptionInfoCollapse");
	setCollapseBind("psychologicalTestClick", "psychologicalTestCollapse");
	setCollapseBind("physicalSymptomClick", "physicalSymptomCollapse");
	setCollapseBind("neuroPsychologicalClick", "neuroPsychologicalCollapse");

	
	//$("#BasicInfocollapse").slideToggle('slow');
	
	$("#DOB").datepicker({dateFormat: 'yy-mm-dd'});
	$("#dateDiagnosed").datepicker({dateFormat: 'yy-mm-dd'});
	
});


//basic patient information
function editBasicInformation() {

	$.post('../Ajax_Requests/updateBasicInformation.php',
		{
			originalPatientId: <?php echo $_GET['PatId'];?>,
			patientId: $("#patientId").val(),
			dateDiagnosed: $("#dateDiagnosed").val(),
			DOB: $("#DOB").val(),
			selectGender: $("#selectGender").val(),
			selectRelationship: $("#selectRelationship").val(),
			selectOccupation: $("#selectOccupation").val(),
			selectTickWorkEnv: $("#selectTickWorkEnv").val(),
			selectTickHuntEnv: $("#selectTickHuntEnv").val(),
			selectTickPlayEnv: $("#selectTickPlayEnv").val(),
			selectTickGardenEnv: $("#selectTickGardenEnv").val(),
			selectTickBite: $("#selectTickBite").val(),
			selectRash: $("#selectRash").val()
		},
		 
		function(data) {

			
			if (data.length > 0) {

				values = new Array();
				values = jQuery.parseJSON(data);

				if (values[0] == 'error') {
					$("#basicInfoClick").html("Basic Information - <span style='color: red;'>Error</span>")
					$.each(values, function(key, value) {
						$("#" + value).css('border-color', 'red');
					});
				}
				else {
					$("#BasicInfocollapse").slideToggle('slow');
					$("#basicInfoClick").html("Basic Information - <span style='color: #CE8111;'>Successfully Updated!</span>");
				}
			}
		}
	);	
}

</script>

</head>
<body>

 	<div id="header">
    	<div class="pageTitleText" style="font-size: 1.5em;">Edit Patient</div>
 	</div>  
    
    
    <div id="mainMenu" style="height: 451px;">
    	<ul style="list-style: none outside none;">
    	
    		<li>
    			<a href="choosePatient.php?SID=7" style="font-weight: bold; color: #7C3709;">Choose New Patient</a>
    		</li>

	        <li>
	         	<a href="Add_ICD9.php?PatId=<?php echo $_GET['PatId'];?>">Patient ICD9</a>
	        </li>
	        
	        <li>
	         	<a href="Add Prescription.php?PatId=<?php echo $_GET['PatId'];?>">Patient Prescription</a>
	        </li>
	        
	        <li>
	        	<a href="Physical Symptoms.php?PatId=<?php echo $_GET['PatId'];?>">Physical Symptoms</a>
	        </li>
	        
	        <li>
	        	<a href="Neuropsychological Symptoms.php?PatId=<?php echo $_GET['PatId'];?>">Neuropsychological Symptoms</a>
	        </li>
	        
	        <li>
	        	<a href="Psychological Test.php?PatId=<?php echo $_GET['PatId'];?>">Psychological Test</a> 
	        </li>
	        
	        <li>
	        	<a href="Diagnostic Test.php?PatId=<?php echo $_GET['PatId'];?>">Diagnostic Test</a>
	        </li>
	        
	        <li>
	         	<a href="viewPatient.php?PatId=<?php echo $_GET['PatId'];?>">View Patient Information</a>
	        </li>
	        
	        <li>
	         	<a href="Edit Patient.php?PatId=<?php echo $_GET['PatId'];?>">Edit Patient Information</a>
	        </li>
	        
	        <li>
	        	<a href="Save Information.php?PatId=<?php echo $_GET['PatId'];?>">Save Patient Information</a>
	        </li>
	        
	        <li style="border-bottom: 1px solid #FD9D45;">
	       	 	<del><!--<a href="query.php">-->Query<!--</a>--></del>
	        </li>
	        
	        <li>
	       	 	<a href="home.php">Home</a>
	        </li> 
	         
	        <li>
	       	 	<a href="logout.php">Logout</a>
	        </li>
        </ul>
    </div>  
    

     <div class="viewOptions">
    		<span class="loginButton">
	       	 	<a href="viewPatient.php?PatId=<?php echo $_GET['PatId'];?>">View Information</a>
	        </span>
    </div>
    
    
    
   <div class="formContainer">
        <!--<form action="../Ajax_Requests/updateBasicInformation.php" method="post">-->
         <form action="<?php echo $_SERVER["PHP_SELF"] . '?PatId=' . $_GET['PatId']; ?>" method="post">
			<input type="hidden" name="isPostBack" value="true" />
        	
        	<div class="boxStyle">
        	<div class="boxStyleTitle" id="basicInfoClick">Basic Information</div>
	            <div class="inboxStyle" id="BasicInfocollapse">
		            <div id="basicInfoTable">
			             <table class="tableClass" border="1">
			                
			                <tr>
			                	<th>Patient Id</th>
			                	<td>
									<input type="text" id="patientId" name="patientId" value="<?php echo $patientInfo['PatID'];?>" style="text-align:center;" onChange="checkPatientId($(this).val())"/>
								</td>
			                </tr>
			                
			                <tr>
			                	<th>Date Diagnosed</th>
			                	<td>
			                		<input type="text" id="dateDiagnosed" name="dateDiagnosed" value="<?php echo $patientInfo['DateDiagnosed'];?>" style="text-align:center;"/>
			                	</td>
			                </tr>
			                
			                <tr>
			                	<th>Date of Birth</th>
			                	<td>
			                		<input type="text" id="DOB" name="DOB" value="<?php echo $patientInfo['DOB'];?>" style="text-align:center;"/>
			                	</td>
			                </tr>
			                
			                <tr>
			                	<th>Gender</th>
			                	<td><select id="selectGender" name="selectGender">
				                		<option value="M" 
				                			<?php if ($patientInfo['Gender'] == 'M') 
				                					echo 'selected="selected"';?>
				                		>M</option>
				                		<option value="F" 
				                			<?php if ($patientInfo['Gender'] == 'F') 
				                					echo 'selected="selected"';?>
				                		>F</option>
									</select>
								</td>
			                </tr>
			                
			                <tr>
			                	<th>Relationship Status</th>
			                	<td><select id="selectRelationship" name="selectRelationship">
				                		<option value="0" 
				                			<?php if (isset($_POST['selectRelationship'])) {
				                						if ($_POST['selectRelationship'] == 0)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['RelationshipStatus'] == 0) 
				                							echo 'selected="selected"';?>
				                		>Single</option>
				                		<option value="1" 
				                			<?php if (isset($_POST['selectRelationship'])) {
				                						if ($_POST['selectRelationship'] == 1)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['RelationshipStatus'] == 1) 
				                							echo 'selected="selected"';?>
				                		>In a Relationship</option>
									</select>
								</td>
			                </tr>
			                
			                <tr>
			                	<th>Occupation Status</th>
			                	<td><select id="selectOccupation" name="selectOccupation">
				                		<option value="0" 
				                			<?php if (isset($_POST['selectOccupation'])) {
				                						if ($_POST['selectOccupation'] == 0)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['OccupationStatus'] == 0) 
				                							echo 'selected="selected"';?>
				                		>Unemployed</option>
				                		<option value="1" 
				                			<?php if (isset($_POST['selectOccupation'])) {
				                						if ($_POST['selectOccupation'] == 1)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['OccupationStatus'] == 1) 
				                							echo 'selected="selected"';?>
				                		>Employed</option>
									</select>
								</td>
			                </tr>
               				         
               				                				      			
			                <tr id="ticksInWorkEnv">
			                	<th>Ticks in Work Environment</th>
			                	<td><select id="selectTickWorkEnv" name="selectTickWorkEnv">
				                		<option value="0" 
				                			<?php if (isset($_POST['selectTickWorkEnv'])) {
				                						if ($_POST['selectTickWorkEnv'] == 0)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['WorkHabitat'] == 0) 
				                							echo 'selected="selected"';?>
				                		>No</option>
				                		<option value="1" 
				                			<?php if (isset($_POST['selectTickWorkEnv'])) {
				                						if ($_POST['selectTickWorkEnv'] == 1)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['WorkHabitat'] == 1) 
				                							echo 'selected="selected"';?>
				                		>Yes</option>
									</select>
								</td>
			                </tr>
			    
			    
			                <tr>
			                	<th>Ticks in Hunting Environment</th>
			                	<td><select id="selectTickHuntEnv" name="selectTickHuntEnv">
				                		<option value="0" 
				                			<?php if (isset($_POST['selectTickHuntEnv'])) {
				                						if ($_POST['selectTickHuntEnv'] == 0)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['HuntHabitat'] == 0) 
				                							echo 'selected="selected"';?>
				                		>No</option>
				                		<option value="1" 
				                			<?php if (isset($_POST['selectTickHuntEnv'])) {
				                						if ($_POST['selectTickHuntEnv'] == 1)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['HuntHabitat'] == 1) 
				                							echo 'selected="selected"';?>
				                		>Yes</option>
									</select>
								</td>
			                </tr>
			                
			                 <tr>
			                	<th>Ticks in Play Environment</th>
			                	<td><select id="selectTickPlayEnv" name="selectTickPlayEnv">
				                		<option value="0" 
				                			<?php if (isset($_POST['selectTickPlayEnv'])) {
				                						if ($_POST['selectTickPlayEnv'] == 0)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['PlayHabitat'] == 0) 
				                							echo 'selected="selected"';?>
				                		>No</option>
				                		<option value="1" 
				                			<?php if (isset($_POST['selectTickPlayEnv'])) {
				                						if ($_POST['selectTickPlayEnv'] == 1)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['PlayHabitat'] == 1) 
				                							echo 'selected="selected"';?>
				                		>Yes</option>
									</select>
								</td>
			                </tr>
			                
			                <tr>
			                	<th>Ticks in Garden Environment</th>
			                	<td><select id="selectTickGardenEnv" name="selectTickGardenEnv">
				                		<option value="0" 
				                			<?php if (isset($_POST['selectTickGardenEnv'])) {
				                						if ($_POST['selectTickGardenEnv'] == 0)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['GardenHabitat'] == 0) 
				                							echo 'selected="selected"';?>
				                		>No</option>
				                		<option value="1" 
				                			<?php if (isset($_POST['selectTickGardenEnv'])) {
				                						if ($_POST['selectTickGardenEnv'] == 1)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['GardenHabitat'] == 1) 
				                							echo 'selected="selected"';?>
				                		>Yes</option>
									</select>
								</td>
			                </tr>
			                <tr>
			                	<th>Tickbite Caused Original Disease</th>
			                	<td><select id="selectTickBite" name="selectTickBite">
				                		<option value="0" 
				                			<?php if (isset($_POST['selectTickBite'])) {
				                						if ($_POST['selectTickBite'] == 0)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['TickBite'] == 0) 
				                							echo 'selected="selected"';?>
				                		>No</option>
				                		<option value="1" 
				                			<?php if (isset($_POST['selectTickBite'])) {
				                						if ($_POST['selectTickBite'] == 1)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['TickBite'] == 1) 
				                							echo 'selected="selected"';?>
				                		>Yes</option>
									</select>
								</td>
			                </tr>
			                <tr>
			                	<th>Currently Has a Rash</th>
			                	<td><select id="selectRash" name="selectRash">
				                		<option value="0" 
				                			<?php if (isset($_POST['selectRash'])) {
				                						if ($_POST['selectRash'] == 0)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['Rash'] == 0) 
				                							echo 'selected="selected"';?>
				                		>No</option>
				                		<option value="1" 
				                			<?php if (isset($_POST['selectRash'])) {
				                						if ($_POST['selectRash'] == 1)
				                								echo 'selected="selected"';
				                					  } elseif ($patientInfo['Rash'] == 1) 
				                							echo 'selected="selected"';?>
				                		>Yes</option>
									</select>
								</td>
			                </tr>
						</table>
					</div>
				</div>
			</div>
            
            
            <input type="button" value="Submit Values" class="loginButton" onclick="editBasicInformation()" style="margin-top: 0px; margin-bottom: 20px;" />


			
			<div class="boxStyle">
	        	<div class="boxStyleTitle" id="diagnosticInfoClick">ICD9 Information</div>
		            <div class="inboxStyle" id="diagnosticInfoCollapse">
			            <div class="tableDiv">
			            <?php if(count($icd_objects) > 0) { //if the results pulled from the database are not null?>
			           		<table class="tableClass">
			                     
			                    <tr style="background-color: #FFB93E;">
				                    <th>Code/Name</th>
				                    <th>Date Assigned</th>
			            			<th>Submit</th>
			                    </tr>
			                    
			                    <?php $ICDRowColor = 1; // counter for background color change?>
			                   
			                    <?php foreach ($icd_objects as $icdArray) {
			                    
			                    	if ($ICDRowColor % 2 == 0)
			                    		$td = "<td style='background-color:#A1DFE8;'>";
			                    	else
			                    		$td = "<td>";
			                    	
			                    	?>
			                    	
			                    	<tr id="icd<?php echo $icdArray['id'];?>">
			                    		
			                    		<?php echo $td;?>
				                    		<select id="icd_id<?php echo $icdArray['id'];?>" name="icd_name" style="width: 450px;">
								                <?php foreach ($icd_codes as $codeObject) {
								                	$codes = $codeObject->get();
								                	
								                	if ($codes['id'] == $icdArray['icd_id'])
								                		echo '<option value="' . $codes['id'] . '" selected="selected">';
								                	else 
								                		echo '<option value="' . $codes['id'] . '">';
								                	echo $codes['code'] . " : " . $codes['name'];
								                	echo '</option>';
								                }?>
							                </select>
			                    		</td>
			                    	
			                    		<?php echo $td;?>
			                    			<input type="text" id="icdDate<?php echo $icdArray['id'];?>" value="<?php echo $icdArray['date_given'];?>" style="text-align:center;"/>
			                    			<script type="text/javascript">
			                    				$("#icdDate" + <?php echo $icdArray['id'];?>).datepicker({dateFormat: 'yy-mm-dd'});
			                    			</script>
			                    		</td>
			                    	
			                    		<?php echo $td;?>
			                    			<input type="button" value="Submit" class="updateRowButton" id="icdSubmit<?php echo $icdArray['id'];?>" onclick="editICDInformation($(this).closest('tr').attr('id'))"/>
			                    		</td>
			                    		
			                    	</tr>
			                    	
			                    	<?php 	
			                    	$ICDRowColor++; // increases background color counter
			                    }?>
			                    
			                </table>
			                <?php } else {
			                	echo "No Results";
			                }?>
			            </div>
		            </div>
	        </div>	
				
				


            <div class="boxStyle">
	        	<div class="boxStyleTitle" id="diagnosticInfoClick">
	        	Diagnostic Test Information
	        	</div>
		            <div class="inboxStyle" id="diagnosticInfoCollapse">
			            <div class="tableDiv">
			            <?php if($answers1->num_rows >= 1) { //if the results pulled from the database are not null?>
			           		<table class="tableClass">
			                     
			                    <tr style="background-color: #FFB93E;">
			                    <?php while ($columnName = $columnNames1->fetch_assoc()) { ?>
			                    	<th><?php echo $columnName['text'];?></th>
			            		<?php }?>
			            		<th>Submit</th>
			                    </tr>
			                    
			                    <?php $DiagnosticTableColor = 1; // counter for background color change?>
			                   
			                    <?php for ($i = 0; $i < count($diagnosticInformationArray); $i++) {
			                    	if ($DiagnosticTableColor % 2 == 0)	{ ?> 
			                    	 <tr id="diagnostic<?php echo $diagnosticInformationArray[$i][2]['id'];?>">
			                    		<td style="background-color:#A1DFE8; width:40%;">
			                    			<?php echo getTestName($diagnosticInformationArray[$i][2]['testname'])?>
			                    		</td>
			                    		
			                    		<td style="background-color:#A1DFE8;">
			                    			<select name="<?php echo $diagnosticInformationArray[$i][0]['name'];?>" id="<?php echo $diagnosticInformationArray[$i][0]['name'];?>">
			                    				<?php for ($y = 0; $y < count($diagnosticInformationArray[$i][0]['options']); $y++) { 
			                    				$nameValue = $diagnosticInformationArray[$i][0]['options'][$y];
			                    				$value = $diagnosticInformationArray[$i][0]['ans'];
			                    					?>
			                    				<option value="<?php echo $nameValue;?>" <?php if ($value == $nameValue) echo " selected='selected'";?>>
			                    					<?php echo $nameValue;?>
			                    				</option>
			                    				<?php }?>
			                    			</select>
			                    		</td>
			                    		
			                    		<td style="background-color:#A1DFE8;">
			                    			<select name="<?php echo $diagnosticInformationArray[$i][1]['name'];?>" id="<?php echo $diagnosticInformationArray[$i][1]['name'];?>">
			                    				<?php for ($y = 0; $y < count($diagnosticInformationArray[$i][1]['options']); $y++) { 
			                    				$nameValue = $diagnosticInformationArray[$i][1]['options'][$y];
			                    				$value = $diagnosticInformationArray[$i][1]['ans'];
			                    				?>
			                    				<option value="<?php echo $nameValue;?>" <?php if ($value == $nameValue) echo " selected='selected'";?>>
			                    					<?php echo $nameValue;?>
			                    				</option>
			                    				<?php }?>
			                    			</select>
			                    		</td>
			                    		
			                    		<td style="background-color:#A1DFE8;">
			                    			<?php echo $diagnosticInformationArray[$i][2]['record_date'];?>
			                    		</td>
			                    		
			                    		<td style="background-color:#A1DFE8;">
			                    			<input type="button" value="Submit" class="updateRowButton" id="diagnosticSubmit<?php echo $diagnosticInformationArray[$i][2]['id'];?>" onclick="editDiagnosticTestInformation($(this).closest('tr').attr('id'))"/>
			                    		</td>
			                    		
			                    	</tr>
			                    	
			                    	<?php } else { ?>
			                    	
			                    	 <tr id="diagnostic<?php echo $diagnosticInformationArray[$i][2]['id'];?>">
			                    	 
										<td style="width:40%;">
			                    			<?php echo getTestName($diagnosticInformationArray[$i][2]['testname'])?>
			                    		</td>
			                    		
			                    		<td>
			                    			<select name="<?php echo $diagnosticInformationArray[$i][0]['name'];?>" id="<?php echo $diagnosticInformationArray[$i][0]['name'];?>">
			                    				<?php for ($y = 0; $y < count($diagnosticInformationArray[$i][0]['options']); $y++) {
			                    				$nameValue = $diagnosticInformationArray[$i][0]['options'][$y];
			                    				$value = $diagnosticInformationArray[$i][0]['ans'];
			                    				?>
			                    				<option value="<?php echo $nameValue;?>" <?php if ($value == $nameValue) echo " selected='selected'";?>>
			                    					<?php echo $nameValue;?>
			                    				</option>
			                    				<?php }?>
			                    			</select>
			                    		</td>
			                    		
			                    		<td>
			                    			<select name="<?php echo $diagnosticInformationArray[$i][1]['name'];?>" id="<?php echo $diagnosticInformationArray[$i][1]['name'];?>">
			                    				<?php for ($y = 0; $y < count($diagnosticInformationArray[$i][1]['options']); $y++) {
			                    				$nameValue = $diagnosticInformationArray[$i][1]['options'][$y];
			                    				$value = $diagnosticInformationArray[$i][1]['ans'];
			                    				?>
			                    				<option value="<?php echo $nameValue;?>" <?php if ($value == $nameValue) echo " selected='selected'";?>>
			                    					<?php echo $nameValue;?>
			                    				</option>
			                    				<?php }?>
			                    			</select>
			                    		</td>
			                    		
			                    		<td>
			                    			<?php echo $diagnosticInformationArray[$i][2]['record_date'];?>
			                    		</td>
			                    		
			                    		<td>
			                    			<input type="button" value="Submit" class="updateRowButton" id="diagnosticSubmit<?php echo $diagnosticInformationArray[$i][2]['id'];?>" onclick="editDiagnosticTestInformation($(this).closest('tr').attr('id'))"/>
			                    		</td>
			                    		
			                    	</tr>
			                    		
			                    <?php } 	
			                    	$DiagnosticTableColor++; // increases background color counter
			                    }?>
			                    
			                </table>
			                <?php } else {
			                	echo "No Results";
			                }?>
			            </div>
		            </div>
	        </div>
            
            
            

             <div class="boxStyle">
	        	<div class="boxStyleTitle" id="prescriptionInfoClick">
	        	Prescription Information
	        	</div>
					
		            <div class="inboxStyle" id="prescriptionInfoCollapse">
			            <div class="tableDiv">
				            <?php if($answers2->num_rows >= 1) {?>
				            	<table class="tableClass">
				                    
				                    <tr style="background-color: #FFB93E">
				                    <?php while ($columnName = $columnNames2->fetch_assoc()) { ?>
				                    	<th><?php echo $columnName['text'];?></th>
				            		<?php }?>
				            		<th>Submit</th>
				                    </tr>

				                    <?php $prescriptionTableColor = 1; ?>
				                    <?php for ($i = 0; $i < count($prescriptionInformationArray); $i++) {
				                    	if ($prescriptionTableColor % 2 == 0)
											$td = '<td style="background-color:#A1DFE8;">';
										else 
											$td = '<td>';
				                    	?>
				                    	
				                    	<tr id="prescriptionTR<?php echo $prescriptionInformationArray[$i]['id'];?>">
				                    		<?php echo $td;?>
				                    			<?php echo getMedicationById($prescriptionInformationArray[$i]['medid']);?>
				                    		</td>
				                    		<?php echo $td;?>
				                    			<input type="text" id="prescriptionDosage<?php echo $prescriptionInformationArray[$i]['id'];?>" value="<?php echo $prescriptionInformationArray[$i]['dosage'];?>" style="text-align:center;"/>
				                    		</td>
				                    		<?php echo $td;?>
				                    			<input type="text" id="prescriptionStartDate<?php echo $prescriptionInformationArray[$i]['id'];?>" value="<?php echo $prescriptionInformationArray[$i]['startdate'];?>" style="text-align:center;"/>
				                    			<script type="text/javascript">
				                    				$("#prescriptionStartDate" + <?php echo $prescriptionInformationArray[$i]['id'];?>).datepicker({dateFormat: 'yy-mm-dd'});
				                    			</script>			                    			
				                    		</td>
				                    		<?php echo $td;?>
				                    			<input type="text" id="prescriptionEndDate<?php echo $prescriptionInformationArray[$i]['id'];?>" value="<?php echo $prescriptionInformationArray[$i]['enddate'];?>" style="text-align:center;"/>
				                    			<script type="text/javascript">
				                    				$("#prescriptionEndDate" + <?php echo $prescriptionInformationArray[$i]['id'];?>).datepicker({dateFormat: 'yy-mm-dd'});
				                    			</script>
				                    		</td>
				                    		<?php echo $td;?>
			                    				<input type="button" value="Submit" class="updateRowButton" id="prescriptionSubmit<?php echo $prescriptionInformationArray[$i]['id'];?>" onclick="editPrescriptionTestInformation($(this).closest('tr').attr('id'))"/>
			                    			</td>
				                    	</tr>
				                    	
				                    	<?php $prescriptionTableColor++;?>
				                    <?php }?>
				                     
				                </table>
				           <?php } else{
				           	echo "No Results";
				           } ?>
			           </div>
           		</div>
           </div>
            
            
            
            
            <div class="boxStyle" id="psychologicalTestBox">
	        	<div class="boxStyleTitle" id="psychologicalTestClick">
	        	Psychological Test Information
	        	</div>
							
	            <div class="inboxStyle" id="psychologicalTestCollapse">
		             <div class="tableDiv">
		            <?php if($answers3->num_rows >= 1) {?>
		            	<table class="tableClass">
		
		                <tr style="background-color: #FFB93E">
		                <?php while ($columnName = $columnNames3->fetch_assoc()) { ?>
		                	<th><?php echo $columnName['text']?></th>
		            	<?php }?>
		            	<th>Submit</th>
		                </tr>
		                
		                <?php $countPsychInfo = 1;?>
		                <?php for ($i = 0; $i < count($psychologicalInformationArray); $i++) {?>
			                <?php if ($countPsychInfo % 2 == 0)
			                		$td = '<td style="background-color:#A1DFE8;">';
			                	else 
			                		$td = '<td>';
			                	
			                #	print_r($psychologicalInformationArray);
			                #	die();
			                	?>
			                	<tr id="psychologicalTR<?php echo $psychologicalInformationArray[$i]['id'];?>">
			                		<?php echo $td;?>
			                			<select id="psyqnum1Sel<?php echo $psychologicalInformationArray[$i]['id'];?>" style="max-width: 200px;">
											<?php for ($y = 0; $y < count($psychologicalInformationArray[$i]['qnumber1'][1]); $y++) {?>											
												<option value="<?php echo $psychologicalInformationArray[$i]['qnumber1'][1][$y];?>" <?php if ($psychologicalInformationArray[$i]['qnumber1'][0] == $psychologicalInformationArray[$i]['qnumber1'][1][$y]) echo ' selected="selected"'?>>
													<?php echo $psychologicalInformationArray[$i]['qnumber1'][1][$y] . " : " . $answerDescriptions[$psychologicalInformationArray[$i]['qnumber1'][1][$y]];?>
												</option>
											<?php }?>
			                			</select>
									</td>
			                		<?php echo $td;?>
			                			<select id="psyqnum2Sel<?php echo $psychologicalInformationArray[$i]['id'];?>" style="max-width: 200px;">
											<?php for ($y = 0; $y < count($psychologicalInformationArray[$i]['qnumber2'][1]); $y++) {?>											
												<option value="<?php echo $psychologicalInformationArray[$i]['qnumber2'][1][$y];?>" <?php if ($psychologicalInformationArray[$i]['qnumber2'][0] == $psychologicalInformationArray[$i]['qnumber2'][1][$y]) echo ' selected="selected"'?>>
													<?php echo $psychologicalInformationArray[$i]['qnumber2'][1][$y] . " : " . $answerDescriptions[$psychologicalInformationArray[$i]['qnumber2'][1][$y]];?>
												</option>
											<?php }?>
			                			</select>
									</td>
			                		<?php echo $td;?>
			                			<select id="psyqnum3Sel<?php echo $psychologicalInformationArray[$i]['id'];?>">
											<?php for ($y = 0; $y < count($psychologicalInformationArray[$i]['qnumber3'][1]); $y++) {?>											
												<option value="<?php echo $psychologicalInformationArray[$i]['qnumber3'][1][$y];?>" <?php if ($psychologicalInformationArray[$i]['qnumber3'][0] == $psychologicalInformationArray[$i]['qnumber3'][1][$y]) echo ' selected="selected"'?>>
													<?php echo $psychologicalInformationArray[$i]['qnumber3'][1][$y];?>
												</option>
											<?php }?>
			                			</select>
									</td>
									<?php echo $td;?>
			                			<select id="psyGlobalSel<?php echo $psychologicalInformationArray[$i]['id'];?>">
											<?php for ($y = 0; $y < count($psychologicalInformationArray[$i]['global'][1]); $y++) {?>											
												<option value="<?php echo $psychologicalInformationArray[$i]['global'][1][$y];?>" <?php if ($psychologicalInformationArray[$i]['global'][0] == $psychologicalInformationArray[$i]['global'][1][$y]) echo ' selected="selected"'?>>
													<?php echo $psychologicalInformationArray[$i]['global'][1][$y];?>
												</option>
											<?php }?>
			                			</select>
									</td>
									<?php echo $td;?>
				                    			<input type="text" id="psyRecordDate<?php echo $psychologicalInformationArray[$i]['id'];?>" value="<?php echo $psychologicalInformationArray[$i]['record_date'];?>" style="text-align:center;"/>
				                    			<script type="text/javascript">
				                    				$("#psyRecordDate" + <?php echo $psychologicalInformationArray[$i]['id'];?>).datepicker({dateFormat: 'yy-mm-dd'});
				                    			</script>
				                    </td>
									<?php echo $td;?>
			                    		<input type="button" value="Submit" class="updateRowButton" id="prescriptionSubmit<?php echo $prescriptionInformationArray[$i]['id'];?>" onclick="editPsychologicalTestInformation($(this).closest('tr').attr('id'))"/>
			                    	</td>																				                	
			                	</tr>	
		                
		                <?php $countPsychInfo++;
		                }?>
		                
		                </table>
		                 <?php }  else {
		                	echo "No Results";
		                }?>
		            </div>
				</div>
            </div>
            
            
            
           
            <div class="boxStyle" style="position: relative; left: -250px; min-width: 152%;" id="physicalSymtomBox">
	        	<div class="boxStyleTitle" id="physicalSymptomClick">
	        	Physical Symptom Information
	        	</div>
	            <div class="inboxStyle" id="physicalSymptomCollapse">
		             <div class="tableDiv">
		            <?php if($answers4->num_rows >= 1) {?>
		            	<table class="tableClass">
		
		
		                <tr style="background-color: #FFB93E">
		                <?php while ($columnName = $columnNames4->fetch_assoc()) { ?>
		                	<th><?php echo $columnName['text']?></th>
		            	<?php }?>
		            	<th>Submit</th>
		                </tr>
		                
		                	<?php $countPsychInfo = 1; ?>                 
		        			<?php # for every row of answers																?>
		        		 	<?php for ($i = 0; $i < count($physicalSymptomInformationArray); $i ++) { ?>
		        		 	
		        			<?php if ($countPsychInfo % 2 == 0)
		        					$td = '<td style="background-color:#A1DFE8;">';
		        				else 	
		        					$td = '<td>'; ?>
		        		 	 	
		        		 		<tr id="physicalSymptomTR<?php echo $physicalSymptomInformationArray[$i]['id'];?>">
		        		 		
		        		 			<?php # for every question in the row																?>
		        		 			<?php foreach ($physicalSymptomInformationArray[$i]['answers'] as $key => $value) {?>
											<?php echo $td;?>
        		 								<select id="physicalSympSEL<?php echo $physicalSymptomInformationArray[$i]['id'] . "_" . $value['qnumber'];?>">
		        		 							<?php foreach ($value['options'] as $ansoption) {?>
			        		 							<option value="<?php echo $ansoption;?>" <?php if ($ansoption == $value['answer']) echo ' selected="selected"';?>>
			        		 								<?php echo $ansoption;?>
			        		 							</option>	
		        		 							<?php } ?>
	        		 							</select>
        		 							</td>

		        		 			<?php }?>
		        		 			
		        		 			<?php echo $td;?>
		        		 				<input type="text" id="physRecordDate<?php echo $physicalSymptomInformationArray[$i]['id'];?>" value="<?php echo $physicalSymptomInformationArray[$i]['record_date'];?>" style="text-align:center; width: 90px;"/>
				                    	<script type="text/javascript">
		                    				$("#physRecordDate" + <?php echo $physicalSymptomInformationArray[$i]['id'];?>).datepicker({dateFormat: 'yy-mm-dd'});
		                    			</script>
		        		 			</td>
		        		 			
		        		 			<?php echo $td;?>
			                    		<input type="button" value="Submit" class="updateRowButton" id="prescriptionSubmit<?php echo $physicalSymptomInformationArray[$i]['id'];?>" onclick="editPhysicalSyptomInformation($(this).closest('tr').attr('id'))"/>
			                    	</td>	
		        		 		</tr>
		        		 	<?php $countPsychInfo++; ?>
		        		<?php }?>	

		                </table>
		                 <?php }  else {
		                	echo "No Results";
		                }?>
		            </div>
				</div>
            </div>
            
            
            
            <div class="boxStyle" style="position: relative; left: -250px; min-width: 140%;" id="neuroPsychologicalBox">
	        	<div class="boxStyleTitle" id="neuroPsychologicalClick">
	        	Neuro-Psychological Symptom Information
	        	</div>

	            <div class="inboxStyle" id="neuroPsychologicalCollapse">
		             <div class="tableDiv">
		            <?php if($answers5->num_rows >= 1) {?>
		            	<table class="tableClass">
		
		
		                <tr style="background-color: #FFB93E">
		                <?php while ($columnName = $columnNames5->fetch_assoc()) { ?>
		                	<th><?php echo $columnName['text']?></th>
		            	<?php }?>
		            	<th>Submit</th>
		                </tr>
		                
		                	<?php $countPsychInfo = 1; ?>                 
		        			<?php # for every row of answers																?>
		        		 	<?php for ($i = 0; $i < count($neuroPsychologicalSymptomsArray); $i++) { ?>
		        		 	
		        			<?php if ($countPsychInfo % 2 == 0)
		        					$td = '<td style="background-color:#A1DFE8;">';
		        				else 	
		        					$td = '<td>'; ?>
		        		 	 	
		        		 		<tr id="neuroPsychoSymptomTR<?php echo $neuroPsychologicalSymptomsArray[$i]['id'];?>">
		        		 		
		        		 			<?php # for every question in the row																?>
		        		 			<?php foreach ($neuroPsychologicalSymptomsArray[$i]['answers'] as $key => $value) {?>
											<?php echo $td;?>
        		 								<select id="neuroPsychSympSEL<?php echo $neuroPsychologicalSymptomsArray[$i]['id'] . "_" . $value['qnumber'];?>">
		        		 							<?php foreach ($value['options'] as $ansoption) {?>
			        		 							<option value="<?php echo $ansoption;?>" <?php if ($ansoption == $value['answer']) echo ' selected="selected"';?>>
			        		 								<?php echo $ansoption;?>
			        		 							</option>	
		        		 							<?php } ?>
	        		 							</select>
        		 							</td>

		        		 			<?php }?>
		        		 			
		        		 			<?php echo $td;?>
		        		 				<input type="text" id="neuroPsychRecordDate<?php echo $neuroPsychologicalSymptomsArray[$i]['id'];?>" value="<?php echo $neuroPsychologicalSymptomsArray[$i]['record_date'];?>" style="text-align:center; width: 90px;"/>
				                    	<script type="text/javascript">
		                    				$("#neuroPsychRecordDate" + <?php echo $neuroPsychologicalSymptomsArray[$i]['id'];?>).datepicker({dateFormat: 'yy-mm-dd'});
		                    			</script>
		        		 			</td>
		        		 			
		        		 			<?php echo $td;?>
			                    		<input type="button" value="Submit" class="updateRowButton" id="prescriptionSubmit<?php echo $neuroPsychologicalSymptomsArray[$i]['id'];?>" onclick="editNeuroPsychologicalSyptomInformation($(this).closest('tr').attr('id'))"/>
			                    	</td>	
		        		 		</tr>
		        		 	<?php $countPsychInfo++; ?>
		        		<?php }?>	
		                
		                </table>
		                 <?php }  else {
		                	echo "No Results";
		                }?>
		            </div>
				</div>
            </div>
            
            
            
            
 	</form>           
        
   </div>	         
</body>
</html>